package com.yan.arrays;

public class MyArrays {
    public static boolean equals(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static int[] fill(int[] arr, int value) {
        if (arr == null) {
            System.err.println("输入的数据不合法！");
            return new int[0];
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = value;
        }
        return arr;
    }

    public static int[] fill(int[] arr, int fromIndex, int toIndex, int value) {
        if (arr == null || fromIndex > toIndex || fromIndex > arr.length || toIndex > arr.length || fromIndex < 0 || toIndex < 0) {
            System.err.println("输入的数据不合法！");
            return new int[0];
        }
        for (int i = fromIndex; i < toIndex; i++) {
            arr[i] = value;
        }
        return arr;
    }

    public static int[] copyOf(int[] arr , int newLength){
        int[] newArr = new int[newLength];
        for (int i = 0; i < newArr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    public static int[] copyOfRange(int[] original, int from, int to) {
        // 校验数据
        if (original == null || from < 0 || to < from || from > original.length || to > original.length) {
            System.out.println("输入的数据不合法");
            return new int[0];
        }
        int[] newArr = new int[to - from];
        for (int i = 0; i < newArr.length; i++, from++) {
            newArr[i] = original[from];
        }
        return newArr;
    }

}
